package com.TinyBBS.servlet.control;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.TinyBBS.model.GameModel;
import com.TinyBBS.model.ReviewModel;
import com.TinyBBS.model.UserModel;

/**
 * Servlet implementation class DeleteService
 */
public class DeleteService extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession(true);
		String uri = "Login";
		String msg = "";
		if (session.getAttribute("username") != null) {
			String control = request.getParameter("ctrl").trim();
			
			String id = request.getParameter("id").trim();
			System.out.println(id);
			if (control.equals("user")) {
				UserModel userM = new UserModel();
				uri ="UserControl";
				if(userM.removeUser(id)){
					msg = "User removed successfully";
					System.out.println(msg);
				}else{
					msg = "Failed to remove user";
					System.out.println(msg);
				}
			}
			if (control.equals("review")) {
				ReviewModel reviewM = new ReviewModel();
				uri ="ReviewControl";
				if(reviewM.removeReview(id)){
					msg = "Review removed successfully";
					System.out.println(msg);
				}else{
					msg = "Failed to remove review";
					System.out.println(msg);
				}
			}
			if (control.equals("game")) {
				GameModel gameM = new GameModel();
				uri ="ReviewControl";
				if(gameM.removeGame(id)){
					msg = "Game removed successfully";
					System.out.println(msg);
				}else{
					msg = "Failed to remove game";
					System.out.println(msg);
				}
			}
		}
		request.setAttribute("message", msg);
		RequestDispatcher rd = request.getRequestDispatcher(uri);
		rd.forward(request, response);
	}

}
